package org.ratingviewer.ui;

import java.awt.*;

import javax.swing.*;
import javax.swing.JInternalFrame.JDesktopIcon;

public class ScrollableJDesktopPane extends JDesktopPane implements Scrollable {

	private static final long serialVersionUID = 1L;

	public Dimension getPreferredScrollableViewportSize() {
		return new Dimension(450, 450);
	}

	@Override
	public Dimension getPreferredSize() {
		int maxX = 0;
		int maxY = 0;

		JInternalFrame[] frames = getAllFrames();
		for (int i = 0; i < frames.length; i++) {
			JInternalFrame nextFrame = frames[i];

			Rectangle frameRect = null;
			JDesktopIcon desktopIcon = nextFrame.getDesktopIcon();
			if (desktopIcon.isShowing())
				frameRect = desktopIcon.getBounds();
			else
				frameRect = nextFrame.getBounds();

			maxX = Math.max(frameRect.x + frameRect.width, maxX);
			maxY = Math.max(frameRect.y + frameRect.height, maxY);
		}

		return new Dimension(maxX, maxY);
	}

	public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
		return 10;
	}

	public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
		return 10;
	}

	public boolean getScrollableTracksViewportWidth() {
		if (getParent() instanceof JViewport) {
			return (((JViewport) getParent()).getWidth() > getPreferredSize().width);
		}
		return false;
	}

	public boolean getScrollableTracksViewportHeight() {
		if (getParent() instanceof JViewport) {
			return (((JViewport) getParent()).getHeight() > getPreferredSize().height);
		}
		return false;
	}

	@Override
	public void setDesktopManager(DesktopManager dtm) {
		super.setDesktopManager(new ScrollableDesktopManager(dtm));
	}

	class ScrollableDesktopManager implements DesktopManager {
		final DesktopManager dtm;

		ScrollableDesktopManager(DesktopManager d) {
			dtm = d;
		}

		public void openFrame(JInternalFrame f) {
			dtm.openFrame(f);
		}

		public void closeFrame(JInternalFrame f) {
			dtm.closeFrame(f);
		}

		public void maximizeFrame(JInternalFrame f) {
			dtm.maximizeFrame(f);
		}

		public void minimizeFrame(JInternalFrame f) {
			dtm.minimizeFrame(f);
		}

		public void iconifyFrame(JInternalFrame f) {
			dtm.iconifyFrame(f);
		}

		public void deiconifyFrame(JInternalFrame f) {
			dtm.deiconifyFrame(f);
		}

		public void activateFrame(JInternalFrame f) {
			dtm.activateFrame(f);
		}

		public void deactivateFrame(JInternalFrame f) {
			dtm.deactivateFrame(f);
		}

		public void beginDraggingFrame(JComponent f) {
			dtm.beginDraggingFrame(f);
		}

		public void dragFrame(JComponent f, int newX, int newY) {
			dtm.dragFrame(f, newX, newY);
		}

		public void endDraggingFrame(JComponent f) {
			dtm.endDraggingFrame(f);
			SwingUtilities.invokeLater(new Runnable() {

				@Override
				public void run() {
					revalidate();
				}
			});
		}

		public void beginResizingFrame(JComponent f, int direction) {
			dtm.beginResizingFrame(f, direction);
		}

		public void resizeFrame(JComponent f, int newX, int newY, int newWidth, int newHeight) {
			dtm.resizeFrame(f, newX, newY, newWidth, newHeight);
		}

		public void endResizingFrame(JComponent f) {
			dtm.endResizingFrame(f);
			SwingUtilities.invokeLater(new Runnable() {

				@Override
				public void run() {
					revalidate();
				}
			});

		}

		public void setBoundsForFrame(JComponent f, int newX, int newY, int newWidth, int newHeight) {
			dtm.setBoundsForFrame(f, newX, newY, newWidth, newHeight);
		}
	}

}